<%# locals: (merged_stories:) -%>

<%
short_id = merged_stories.first.short_id
classes = [
  :story,
  merged_stories.first.negativity_class,
  merged_stories.first.current_upvoted? && 'upvoted',
  merged_stories.first.current_flagged? && 'flagged',
  merged_stories.first.is_hidden_by_cur_user && 'hidden',
  merged_stories.first.is_saved_by_cur_user && 'saved',
  merged_stories.first.is_deleted? && 'deleted',
].filter(&:present?)
%>

<%# remember: _listdetail replicates a lot of this for lists of stories %>

<%#
  The h-entry, u-report-of, u-url and h-card css classes are necessary
  for the webmentions support. http://microformats.org/wiki/h-entry

  This has apparently useless <span> tags explained in a comment in CSS
  around .negative_1 in CSS (introduced same commit as this comment).
%>
<li id="story_<%= short_id %>" data-shortid="<%= short_id %>" class="<%= classes.join(' ') %>">
<div class="story_liner h-entry">
  <div class="voters">
    <%= link_to (merged_stories.first.show_score_to_user?(@user) ? merged_stories.first.score : '~'),
                (@user ? '': login_path), :class => "upvoter" %>
  </div>

  <% merged_stories.filter {it.can_be_seen_by_user?(@user)}.each do |ms| %>
    <div class="details" id="<%= ms.header_anchor %>">
      <span role="heading" class="link" aria-level="1" class="link h-cite u-repost-of">
        <a href="<%= Routes.url_or_comments_path(ms) %>" rel="ugc <%= ms.send_referrer? ? '' : 'noreferrer' %>" class="u-url"><%= ms.title %></a>
      </span>

      <% if ms.markeddown_description.present? %>
        <a class="description_present" title="<%= truncate(ms.description, :length => 500) %>" href="<%= Routes.title_path ms %>">&#x2636;</a>
      <% end %>

      <span class="tags">
        <% ms.tags.each do |tag| %>
          <a href="<%= tag_path(tag) %>" class="<%= tag.css_class %>" title="<%= tag.description %>"><%= tag.tag %></a>
        <% end %>
      </span>
      <% if ms.origin.present? %>
        <%= link_to ms.origin.identifier, origin_path(ms.origin), class: 'domain' %>
      <% elsif ms.domain.present? %>
        <%= link_to ms.domain.domain, domain_path(ms.domain), class: 'domain' %>
      <% end %>

      <div class="byline">
        <span><%= ms.user_is_author? ? "authored by" : "via" %></span>
        <%= inline_avatar_for @user, ms.user %>
        <%= styled_user_link ms.user, ms, class: ['u-author', 'h-card'] %>

        <% if ms.previewing %>
          <span>just now</span>
        <% else %>
          <%= how_long_ago_label(ms.created_at) %>
        <% end %>

        <% if ms.is_editable_by_user?(@user) %>
          <span> | </span>
          <a href="<%= edit_story_path(ms.short_id) %>">edit</a>
        <% end %>

        <% if @user&.is_moderator? %>
          <span> | </span>
          <a href="<%= edit_mod_story_path(ms.short_id) %>" class="<%= ms.has_suggestions? ? "story_has_suggestions" : "" %>">mod edit</a>
        <% end %>

        <% if ms.can_have_suggestions_from_user?(@user) %>
          <span> | </span>
          <%= link_to "suggest", new_story_suggestion_path(ms.short_id), :class => "suggester" %>
        <% end %>

        <% if !ms.is_gone? && @user %>
          <% if ms.current_flagged? %>
            <span> | </span><a class="flagger">unflag (<%= Vote::STORY_REASONS[ms.current_vote[:reason]].to_s.downcase %>)</a>
          <% elsif @user.can_flag?(ms) %>
            <span> | </span>
            <span class="dropdown_parent">
              <a class="flagger">flag</a>
            </span>
          <% end %>

          <% if ms.is_hidden_by_cur_user %>
            <span> | </span><%= link_to "unhide", story_unhide_path(ms.short_id), :class => "hider" %>
          <% else %>
            <span> | </span><%= link_to "hide", story_hide_path(ms.short_id), :class => "hider" %>
          <% end %>

          <% if ms.hider_count > 0 %>
            <% if ms.is_hidden_by_cur_user %>
              (hidden by you and <%= pluralize(ms.hider_count - 1, "other user") %>)
            <% else %>
              (hidden by <%= pluralize(ms.hider_count, "user") %>)
            <% end %>
          <% end %>

          <% if ms.disownable_by_user?(@user) %>
            |
            <%= link_post "disown", story_disown_path(ms.short_id), { confirm: "Are you sure you want to disown this story?"} %>
          <% end %>

          <% if !ms.is_saved_by_cur_user %>
            <span> | </span><%= link_to "save", story_save_path(ms.short_id), :class => "saver" %>
          <% else %>
            <span> | </span><%= link_to "unsave", story_unsave_path(ms.short_id), :class => "saver" %>
          <% end %>
        <% end %>

        <% if ms.is_gone? %>
          [Story removed by <%= ms.is_moderated? ? "moderator" : "submitter" %>]
        <% else %>
          <% if ms.url.present? && (!ms.is_gone? || @user.try(:is_moderator?))  %>
            <span> | </span>
            <span class="dropdown_parent">
              <input id="archive_<%= ms.short_id %>" class="archive_button" type="checkbox">
              <label for="archive_<%= ms.short_id %>" tabindex="0">caches</label>
              <div class="archive-dropdown">
                <a href="<%= ms.archiveorg_url %>">Archive.org</a>
                <a href="<%= ms.archivetoday_url %>">Archive.today</a>
                <a href="<%= ms.ghost_url %>">Ghostarchive</a>
              </div>
            </span>
          <% end %>

          <span class="comments_label">
            |
            <a role="heading" aria-level="2" href="<%= Routes.title_path(merged_stories.first, anchor: ms.comments_anchor) %>">
              <%= ms.comments_count.zero? ? "no" : ms.comments_count %>
              <%= 'comment'.pluralize(ms.comments_count) %>
            </a>
          </span>

          <% if ((@user&.is_moderator? && ms.flags > 0) || (ms.flags >= 3 || ms.score <= 0)) %>
            <span> | <%= ms.vote_summary_for(@user).downcase %> </span>
          <% end %>
        <% end %>
      </div>
    </div>
  <% end %>

  <% if merged_stories.count > 1 %>
    <div class="details byline">
      <span class="merge"></span><%= merged_stories.count %> merged

      <% if !merged_stories.first.is_gone? || @user&.is_moderator? %>
        <span class="comments_label">
          |
          <% cc = merged_stories.map(&:comments_count).sum %>
          <%= (cc == 0) ? "no" : "total: #{cc}" %> <%= 'comment'.pluralize(cc) %></a>
        </span>
      <% end %>
    </div>
  <% end %>
</div>
</li>
